package xcmg.device.web.agent;


import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.swagger.annotations.ApiOperation;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.agent.ServiceBorrowingService;
import xcmg.device.service.vo.agent.ServiceBorrowingVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;


/**
 * 服务人员借用零件表
 * 
 * @author Xcs
 * @email  666@168.com
 * @date   2019-04-29 09:34:52
 */
@RestController
@RequestMapping("/BorrowUsing")
public class ServiceBorrowingController extends BasicController {
	@Autowired
	private ServiceBorrowingService borrowUsingService;

    /**
     * 详情
     */
    @ApiOperation(value = "服务人员借用零件表详情接口")
    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        ServiceBorrowingVO borrowUsingVO= borrowUsingService.item(id);
        return actionValueResult(borrowUsingVO);
    }

    /**
     * 服务人员借用单详情
     */
    @ApiOperation(value = "服务人员借用单详情")
    @PostMapping(value = "/detail")
    @FastMappingInfo(needLogin = true)
    public ActionResult detail(@RequestBody SearchCommonVO<ServiceBorrowingVO> condition) throws Exception {
        if (null == condition.getFilters() || StringUtils.isBlank(condition.getFilters().getId())){
            return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(borrowUsingService.detail(condition,getOrgId()).getPageInfo());
    }

    /**
     * 列表
     */
    @ApiOperation(value = "服务人员借用零件表列表接口")
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<ServiceBorrowingVO> condition) throws Exception{
        ServiceBorrowingVO filters=condition.getFilters();
        if(filters==null){
            filters=new ServiceBorrowingVO();
        }
        filters.setOrgId(getOrgId());
        condition.setFilters(filters);
        PageCommonVO pageCommonVO=borrowUsingService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    /**
     * 新增
     */
    @ApiOperation(value = "服务人员借用零件表新增接口")
    @RequestMapping(value = "/insert",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult insert(@RequestBody ServiceBorrowingVO borrowUsingVO){
        ErrorCode code = borrowUsingService.insert(borrowUsingVO);
        return actionResult(code);
    }

    /**
     * 更新
     */
    @ApiOperation(value = "服务人员借用零件表更新接口")
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody ServiceBorrowingVO borrowUsingVO){
        ErrorCode code=borrowUsingService.update(borrowUsingVO);
        return actionResult(code);
    }

    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出")
    public void export(String[] ids)throws Exception {
        borrowUsingService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出全部")
    public void exportAll(ServiceBorrowingVO condition)throws Exception {
        if (null == condition){
            condition = new ServiceBorrowingVO();
        }
        condition.setOrgId(getOrgId());
        borrowUsingService.exportAll(condition);
    }

    @RequestMapping(value = "/exportAllDetail",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出全部详情")
    public void exportAllDetail(ServiceBorrowingVO condition)throws Exception {
        borrowUsingService.exportAllDetail(condition,getOrgId());
    }

    /**
     * 列表
     */
    @ApiOperation(value = "服务人员借用零件统计总金额接口")
    @RequestMapping(value = "/totalAmountList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public ActionResult totalAmountList(@RequestBody SearchCommonVO<ServiceBorrowingVO> condition) throws Exception{
        ServiceBorrowingVO filters=condition.getFilters();
        if(filters==null){
            filters=new ServiceBorrowingVO();
        }
        filters.setOrgId(getOrgId());
        condition.setFilters(filters);
        PageCommonVO pageCommonVO=borrowUsingService.totalAmountList(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/exportAllTotalAmountList",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    @ApiOperation(value = "导出全部服务人员借用零件统计总金额接口")
    public void exportAllTotalAmountList(ServiceBorrowingVO condition)throws Exception {
        if (null == condition){
            condition = new ServiceBorrowingVO();
        }
        condition.setOrgId(getOrgId());
        borrowUsingService.exportAllTotalAmountList(condition);
    }
}
